/**
 * Created by Administrator on 2016/11/8.
 */
$(function(){
    var cArr = [];
    //生成侧边导航
    _inita();

    //初始化隐藏所有子节
    //$(".slider-ul li.li-title").nextUntil(".li-title").css("display","none");
    //$("#scroll-spy").height(450);
    var len =$(".slider-ul li").length + 1;
    $("#scroll-spy").height(len*30);

    //第一次打开默认展示第一项
    // var w_url=window.location.href;
    //
    // var i = w_url.indexOf("#");
    // if (i == -1) {
    // 	$(".slider-ul li."+1+"").css("display","");
    // }

    // $(window).on('scroll',function(event) {
    //   event.preventDefault();
    //   var scroll_height = $(this).scrollTop();
    //   if (scroll_height > 300) {
    //     $('#scroll-spy').addClass('affix-fixed');
    //   }else {
    //     $('#scroll-spy').removeClass('affix-fixed');
    //   }
    // });

    //生成导航a
    function _inita(){
        // var $atitle = $('.cnt-main-body-1 .cnt-main-body-hd'),
        //     $btitle = $('.section-header'),
        var $atitle = $('.cnt-main-body-1-prev'),
            $btitle = $('.cnt-main-body-1 .cnt-main-body-hd'),
            aArr = [],
            bArr = [];

        if ($atitle.length == 0 && $btitle.length == 0) {   //为零则取消侧栏
            $('.cnt-aside').remove();
            // $('.cnt-main').removeClass('col-xs-10').addClass('col-xs-12');
            return;
        };


        //一级标题
        $atitle.each(function() {
            aArr.push($(this).attr('id'));
        });

        //二级标题
        $btitle.each(function() {
            bArr.push($(this).attr('id'));
        });

        // var aArr = aArr.concat(bArr).sort();


        var dom = '';//导航a HTML
        for (var i = 1; i < aArr.length+1; i++) {
            var txt = getTxt(0,aArr[i-1]);
            dom += '<li class="'+aArr[i-1]+' li-title"><a href="#'+aArr[i-1]+'">'+txt+'</a></li>';
            cArr.push(aArr[i-1]);
            for(var j = 1; j < bArr.length+1; j++){
                if($("#"+aArr[i-1]).parent().find("#"+bArr[j-1]).length > 0){
                    txt = getTxt(1, bArr[j-1]);
                    dom += '<li class="'+bArr[j-1]+' li-second-title"><a href="#'+bArr[j-1]+'">'+txt+'</a></li>';
                    cArr.push(bArr[j-1]);
                }
            }

        }

        $('.slider-ul').empty().append(dom);
    }

    //根据id获取文字
    function getTxt(type,name){
        var txt = '';
        if (type == 0) {
            txt = $('.cnt-main div[id="'+name+'"]').parent().find(".cnt-main-body-hd").eq(0).text();
            txt = txt.substring(0, txt.indexOf("—"));
        }else {
            txt = $('.cnt-main div[id="'+name+'"]').text();
            txt = txt.substring(txt.indexOf("—")+1);
        }
        return txt;

    }


    $("#sliderUl li a").click(function(){
        $(this).parent().parent().find("li").removeClass("active");
        $(this).parent().addClass("active")
    })
    $(window).scroll(function(){
        var scrollTop = $(window).scrollTop();
        var sliderUl = $(".slider-ul li");
        if(scrollTop < $("[id='"+"sczrxx"+"']").offset().top - 30)
        {
            $(".slider-ul li:nth-child(1)").removeClass("active");
        }else{
            // $(".cnt-nav").removeClass("nav-fixed");
        }
        // 滑动样式
        if ($(document).scrollTop() >= 343) {
            $("#scroll-spy").css({"position": "fixed", "top": 25});
        }else{
            $("#scroll-spy").css({"position": "relative"});
        }
        if ($(document).scrollTop() >= $(".cnt-main").offset().top + $(".cnt-main").outerHeight() - $("#scroll-spy").outerHeight() - 40) {
            $("#scroll-spy").css({"position": "relative"});
        };
        for(var i=1; i<=sliderUl.size(); i++){
            if ($("[id="+cArr[i-1]+"]").offset().top - 30 <= $(document).scrollTop()) {
                if($(".slider-ul li:nth-child("+i+")").attr("class").indexOf("active") == -1 && $(document).height() !== $(this).height() + $(this).scrollTop()){
                    $(".slider-ul li:nth-child("+i+") a").click();
                }
            };
        }

    });

})